package classes;

import com.mysql.jdbc.Connection;
import Servlet.dataBaseController;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Brian
 */
public class MostRevenue {
    private static Connection con=null;
    private static ResultSet rs=null;
    private static String queryCust="SELECT P.FirstName,P.LastName, A.Id, (S.PricePerShare-R.PricePerShare) * O.NumShares AS Revenue"+
                            " FROM `brchung`.`person` P,`brchung`.`stock` S, `brchung`.`transaction` R, `brchung`.`order1` O, `brchung`.`trade` T, `brchung`.`account` A"+
                            " WHERE A.Client=P.SSN AND A.ID = T.AccountID AND T.TransactionID = R.ID AND T.StockID = S.StockSymbol AND T.AccountID = A.ID AND O.ID = T.OrderID AND R.PricePerShare < S.PricePerShare"+
                            " Group By P.FirstName"+
                            " ORDER BY Revenue DESC LIMIT 1;";
    
    private static String queryEmp="SELECT P.FirstName, P.LastName, T.BrokerID, (S.PricePerShare-R.PricePerShare) * O.NumShares AS Revenue"+
                            " FROM `brchung`.`stock` S, `brchung`.`transaction` R, `brchung`.`order1` O, `brchung`.`trade` T, `brchung`.`account` A, `brchung`.`person` P"+
                            " WHERE A.ID = T.AccountID AND T.TransactionID = R.ID AND T.StockID = S.StockSymbol AND T.AccountID = A.ID AND O.ID = T.OrderID AND P.SSN=T.BrokerID AND R.PricePerShare < S.PricePerShare"+
                            " Group By T.BrokerID"+
                            " Order By T.BrokerID"+
                            " LIMIT 1;";
    public MostRevenue()
    {}
    public static void getCustConnection() throws SQLException
    {
        try{
            con=dataBaseController.getConnection();
        try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MostRevenue.class.getName()).log(Level.SEVERE, null, ex);
            }
            Statement st = (Statement) con.createStatement();
            rs=st.executeQuery(queryCust);
            
            //st.close();
            //con.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
    }
    public static void getEmpConnection() throws SQLException
    {
        try{
            con=dataBaseController.getConnection();
        try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MostRevenue.class.getName()).log(Level.SEVERE, null, ex);
            }
            Statement st = (Statement) con.createStatement();
            rs=st.executeQuery(queryEmp);
            
            //st.close();
            //con.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    public static ArrayList getResults()
    {
        ArrayList<String> al= new ArrayList<String>();
        try{
            while(rs.next())
            {
                for(int i=0;i<4;i++)
                {
                    al.add(rs.getString(i+1));
                }
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return al;
    }
    
}
